package com.ziyear.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ziyear.common.util.JDBCUtil;
import com.ziyear.common.util.TrainUtils;
import com.ziyear.dao.TrainDao;
import com.ziyear.dao.TrainSeatDao;
import com.ziyear.dao.impl.TrainDaoImpl;
import com.ziyear.dao.impl.TrainSeatDaoImpl;
import com.ziyear.domain.Prep;
import com.ziyear.domain.Train;
import com.ziyear.domain.TrainSeat;
import com.ziyear.service.PrepService;
import com.ziyear.service.TrainService;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.List;


public class TrainServiceImpl implements TrainService {

    private TrainDao trainDao = new TrainDaoImpl();
    private TrainSeatDao trainSeatDao = new TrainSeatDaoImpl();
    private static Logger logger = Logger.getLogger(TrainServiceImpl.class);

    @Override
    public int add(Train train) {

        int recordNumber = 0;
        if (train != null) {
            try {
                JDBCUtil.startTransaction();
                recordNumber = trainDao.add(train);
                List<TrainSeat> trainSeats = TrainUtils.buildTrainSeat(train);
                trainSeatDao.add(trainSeats);
                JDBCUtil.commit();
            } catch (SQLException e) {
                JDBCUtil.rollback();
                logger.error(e.toString());
                e.printStackTrace();
            }
        } else {
            logger.debug("TrainServiceImpl.add 中 train为null");
        }
        return recordNumber;
    }

    @Override
    public int delete(Integer trainId) {
        int recordNumber = 0;
        PrepService prepService = new PrepServiceImpl();
        if (trainId != null && trainId != 0) {
            try {
                List<Prep> prepList = prepService.queryByTrainId(trainId);
                if (CollectionUtil.isNotEmpty(prepList)) {
                    logger.error("已经销售的车次信息不能删除!!!!!!!");
                    return 0;
                }
                recordNumber = trainDao.delete(trainId);
            } catch (SQLException e) {
                logger.error(e.toString());
                e.printStackTrace();
            }
        } else {
            logger.debug("TrainServiceImpl.delete 中 trainId为null或0");
        }
        return recordNumber;
    }

    @Override
    public int update(Train train) {
        int recordNumber = 0;
        if (train != null) {
            try {
                recordNumber = trainDao.update(train);
            } catch (SQLException e) {
                logger.error(e.toString());
                e.printStackTrace();
            }
        } else {
            logger.debug("TrainServiceImpl.update 中 train为null");
        }
        return recordNumber;
    }

    @Override
    public List<Train> getAll() {
        List<Train> list = null;
        try {
            list = trainDao.getAll();
        } catch (SQLException e) {
            logger.error(e.toString());
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Train getById(Integer trainId) {
        Train train = null;
        if (trainId != null && trainId != 0) {
            try {
                train = trainDao.getById(trainId);
            } catch (SQLException e) {
                logger.error(e.toString());
                e.printStackTrace();
            }
        } else {
            logger.debug("TrainServiceImpl.getById 中 trainId为null或0");
        }
        return train;
    }

    @Override
    public List<Train> getByTrainNumber(String trainNumber) {
        List<Train> list = null;
        if (trainNumber != null && !"".equals(trainNumber)) {
            try {
                list = trainDao.getByTrainNumber(trainNumber);
            } catch (SQLException e) {
                logger.error(e.toString());
                e.printStackTrace();
            }
        }
        return list;
    }

    @Override
    public List<Train> getByStartEndStation(String startStation, String endStation, String startTime) {
        List<Train> list = null;
        if (startStation != null && !"".equals(startStation) && endStation != null && !"".equals(endStation)
                && startTime != null && !"".equals(startTime)) {

            try {
                list = trainDao.getByStartEndStation(startStation, endStation, startTime);
            } catch (SQLException e) {
                logger.error(e.toString());
                e.printStackTrace();
            }

        } else {
            logger.debug("TrainServiceImpl.getByStartEndStation: 信息不完整！！！");
        }
        return list;
    }

    @Override
    public List<Train> getAllStartStation() {
        List<Train> list = null;
        try {
            list = trainDao.getAllStartStation();
        } catch (SQLException e) {
            logger.error(e.toString());
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Train> getAllEndStation() {
        List<Train> list = null;
        try {
            list = trainDao.getAllEndStation();
        } catch (SQLException e) {
            logger.error(e.toString());
            e.printStackTrace();
        }
        return list;
    }

}
